﻿using machine_registration.ViewModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace machine_registration.Models
{
    [Table("device_inventory")]
    public partial class DeviceInventory : ObservableValidator
    {
        /// <summary>
        /// 库存设备唯一标识 
        /// </summary>
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int InventoryId { get; set; }


        /// <summary>
        /// SN号
        /// </summary>
        [ObservableProperty]
        [StringLength(100, MinimumLength = 1,ErrorMessage = "SN不能为空")]
        [RegularExpression(@"^[a-zA-Z0-9]+$", ErrorMessage = "设备SN格式不正确,只能输入纯数字和字母）")]
        private string _MachineId = string.Empty;
        /// <summary>
        /// 编号
        /// </summary>
        [ObservableProperty]
        [StringLength(50, ErrorMessage = "序列号长度不能超过50个字符")]
        private string? _SerialNumber;

        /// <summary>
        /// 设备类型（与借出记录枚举一致）
        /// </summary>
        [Required(ErrorMessage = "必须选择设备类型")]
        [ObservableProperty]
        public DeviceType _DeviceType;

        /// <summary>
        /// 设备名称 
        /// </summary>
        [Required(ErrorMessage = "设备名称不能为空")]
        [StringLength(100, MinimumLength = 1)]
        [ObservableProperty]
        public string _DeviceName =string.Empty;

        /// <summary>
        /// 设备编号
        /// </summary>
        [StringLength(50, ErrorMessage = "序列号长度不能超过50个字符")]
        [ObservableProperty]
        public string? _DeviceNumber = string.Empty;

        /// <summary>
        /// 设备使用状态 (在库可借,已借出,等待归还,归还完成,维护中,已报废)
        /// </summary>
        [ObservableProperty]
        public InventoryStatus? _Status ;

     


        /// <summary>
        /// 是否已注销（逻辑删除标记）
        /// </summary>
        [ObservableProperty]
        private bool _IsRetired;

        /// <summary>
        /// 入库时间 
        /// </summary>
        [Required]
        [ObservableProperty]
        public DateTime _EntryDate = DateTime.Now;

        /// <summary>
        /// 设备注销时间 
        /// </summary>
        [Required]
        [ObservableProperty]
        public DateTime _RetiredDate;

        /// <summary>
        /// 来源编号 
        /// </summary>
        [StringLength(50)]
        [ObservableProperty]
        public string? _SourceNumber;

        /// <summary>
        /// 任务名称相关信息 
        /// </summary>
        [StringLength(200)]
        [ObservableProperty]
        public string? _TaskName;

        /// <summary>
        /// 实际归属人
        /// </summary>
        [ObservableProperty]
        [StringLength(100, MinimumLength = 1, ErrorMessage = "实际归属人不能为空")]
        public string? _OwnerId = string.Empty;

        /// <summary>
        /// 当前责任人（与借出记录关联）
        /// </summary>
        [StringLength(50)]
        [ObservableProperty]
        public string? _CurrentKeeper;

        /// <summary>
        /// 最近维护盘点日期 
        /// </summary>
        [ObservableProperty]
        public DateTime? _LastMaintenanceDate;

        /// <summary>
        /// 备注信息
        /// </summary>
        [ObservableProperty]
        public string? _Remarks;
        /// <summary>
        /// 导航属性 - 借出记录 
        /// </summary>
        public virtual ICollection<BorrowRecord> BorrowRecords { get; set; } = new List<BorrowRecord>();

        /// <summary>
        ///   Model 层验证
        /// </summary>
        /// <param name="value"></param>
        public (bool isok, string erro) ValidateProperties()
        {

            // 触发 Record 模型验证
            ValidateAllProperties();

            // 合并错误信息
            var errors = new List<string>();

            if (HasErrors)
            {
                errors.AddRange(GetErrors().Select(e => e.ErrorMessage));
            }



            // 显示错误
            if (errors.Count > 0)
            {

                return (false, string.Join(",", errors));
            }
            return (true, "");
        }

    }
    /// <summary>
    /// 设备类型枚举
    /// </summary>
    public enum DeviceType
    {
        /// <summary>
        /// 手表
        /// </summary>
        [Description("手表")] SmartWatch,
        /// <summary>
        /// 平板
        /// </summary>
        [Description("平板")] Tablet,
       
        /// <summary>
        /// 手机
        /// </summary>
        [Description("手机")] RuggedPhone,
        /// <summary>
        /// 扫描笔
        /// </summary>
        [Description("扫描笔")] Scanner,
        /// <summary>
        ///     其他
        /// </summary>
        [Description("其他")] Other
    }
    /// <summary>
    /// 库存状态枚举
    /// </summary>
    public enum InventoryStatus
    {
        /// <summary>
        /// 在库可借
        /// </summary>
        [Description("在库可借")]
        InStock,        // 设备在库可借 
        /// <summary>
        /// 已借出
        /// </summary>
        [Description("等待归还")]
        Pending,         // 设备已借出 
        /// <summary>
        /// 归还完成
        /// </summary>
        [Description("归还完成")]
        Completed,
        /// <summary>
        /// 维护中
        /// </summary>
        [Description("维护中")]
        UnderMaintenance, // 设备不可借 
        /// <summary>
        /// 已报废,或归还仓库
        /// </summary>
        [Description("已报废")]
        Retired         // 设备已注销 
    }

}
